﻿Imports Persistencia
Public Class frmSetores

#Region "Declaração de variáveis"
    Private objSetores As New Controle.ctrSetores
    Private iIdSetor As Int32
    Private sMensagemValidacao As String = ""
#End Region

#Region "Propriedades"

    Public Property IDSetor() As Int32
        Get
            Return Me.iIdSetor
        End Get
        Set(ByVal value As Int32)
            Me.iIdSetor = value
        End Set
    End Property

#End Region

#Region "Métodos"

    Private Sub frmSetores_alteraModo(ByVal eModoAtual As frmPadrao.eModo) Handles Me.alteraModo
        If (eModoAtual = eModo.eAguardar Or eModoAtual = eModo.eExcluir) Then
            Me.lblCodigo.Enabled = True
            Me.txtIdSetor.Enabled = True
            Me.cmdProcura.Enabled = True
            Me.lblSetor.Enabled = False
            Me.txtSetor.Enabled = False
            Me.chkRealizaVisitas.Enabled = False
            Me.chkRealizaContatos.Enabled = False
        Else
            Me.lblCodigo.Enabled = False
            Me.txtIdSetor.Enabled = False
            Me.cmdProcura.Enabled = False
            Me.lblSetor.Enabled = True
            Me.txtSetor.Enabled = True
            Me.chkRealizaVisitas.Enabled = True
            Me.chkRealizaContatos.Enabled = True
        End If
    End Sub

    Private Sub frmSetores_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Me.txtIdSetor.Focus()
        modoAtual = eModo.eAguardar

    End Sub

    Private Sub cmdGravar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGravar.Click
        Dim bRetorno As Boolean
        Try

            If Me.validarFormulario Then
                bRetorno = Me.objSetores.salvar(Me.IDSetor, _
                                                Me.txtSetor.Text, _
                                                Me.chkRealizaVisitas.Checked, _
                                                Me.chkRealizaContatos.Checked)
                If bRetorno Then
                    MyBase.Codigo = Me.objSetores.IDSetor
                    Me.IDSetor = MyBase.Codigo
                    MyBase.modoAtual = eModo.eAguardar
                    Me.txtIdSetor.Text = Me.IDSetor
                End If
            Else
                MsgBox(Me.sMensagemValidacao, MsgBoxStyle.Exclamation, Me.Text)
                Me.cmdCancelar.Text = "Cancelar"
                MyBase.modoAtual = eModo.eEditar
                Exit Sub
            End If

        Catch ex As Exception
            Me.Limpar(Me)
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    Private Sub cmdExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExcluir.Click
        Try
            If MyBase.Exclui Then
                If Me.objSetores.excluir(Convert.ToInt32(Me.txtIdSetor.Text)) Then
                    MyBase.Codigo = 0
                    Me.IDSetor = 0
                    MyBase.Limpar(Me)
                    MyBase.modoAtual = eModo.eAguardar
                    Me.txtIdSetor.Focus()
                Else
                    MsgBox("Exclusão cancelada. Existem registros vinculados", MsgBoxStyle.Exclamation, Me.Text)
                    MyBase.modoAtual = eModo.eAguardar
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            MyBase.Codigo = 0
            Me.IDSetor = 0
            MyBase.modoAtual = eModo.eAguardar
            Me.Limpar(Me)
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    Private Sub cmdNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNovo.Click
        Me.IDSetor = 0
        Me.txtSetor.Focus()
    End Sub

    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        If Me.cmdCancelar.Text = "Alterar" Then
            Me.txtIdSetor.Focus()
        Else
            Me.txtSetor.Focus()
        End If
    End Sub

    Private Function validarFormulario() As Boolean

        Dim bRetorno As Boolean = True

        If Me.txtSetor.Text = String.Empty Then
            sMensagemValidacao = "- Setor obrigatório."
            bRetorno = False
        End If

        Return bRetorno

    End Function

    Private Sub carregaDados()
        Dim drDados As DataRow

        Try
            If Me.IDSetor > 0 Then
                drDados = Me.objSetores.selecionarSetores(Me.IDSetor)
                Me.txtSetor.Text = drDados.Item("Setor")
                Me.chkRealizaVisitas.Checked = perConversao.ToBoolean(drDados.Item("RealizaVisitas"))
                Me.chkRealizaContatos.Checked = perConversao.ToBoolean(drDados.Item("RealizaContatos"))
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub txtIdSetor_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIdSetor.LostFocus
        Dim drDados As DataRow

        Try
            If Me.txtIdSetor.Text <> String.Empty Then
                drDados = Me.objSetores.selecionarSetores(Convert.ToInt32(Me.txtIdSetor.Text))
                If Not drDados Is Nothing Then
                    Me.txtSetor.Text = drDados.Item("Setor")
                    Me.chkRealizaVisitas.Checked = perConversao.ToBoolean(drDados.Item("RealizaVisitas"))
                    Me.chkRealizaContatos.Checked = perConversao.ToBoolean(drDados.Item("RealizaContatos"))
                    MyBase.Codigo = drDados.Item("IDSetor")
                    Me.IDSetor = MyBase.Codigo
                    MyBase.modoAtual = eModo.eAguardar
                Else
                    MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                    Exit Sub
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub cmdProcura_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcura.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        sql = Me.objSetores.sqlConsulta

        With frmDialogo
            .Campos = "iIDSetor,sSetor"
            .Descricoes = "Código,Descricao"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa de Setores"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    MyBase.Codigo = .Id
                    Me.IDSetor = MyBase.Codigo
                    Me.txtIdSetor.Text = Me.IDSetor
                    Me.carregaDados()
                    Me.modoAtual = eModo.eAguardar
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If

        End With
    End Sub

#End Region
End Class
